using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
using ReflectionStudio.Core.Events;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class VQORDER
    {
        public readonly static VQORDER Instance = new VQORDER();
        private string _YCOUNT;
        public string YCOUNT
        {
            get { return _YCOUNT; }
            set { _YCOUNT = value; OnPropertyChanged("YCOUNT", value); }
        }
        private string _QCORDER;
        public string QCORDER
        {
            get { return _QCORDER; }
            set { _QCORDER = value; OnPropertyChanged("QCORDER", value); }
        }
        private string _QPLAN;
        public string QPLAN
        {
            get { return _QPLAN; }
            set { _QPLAN = value; OnPropertyChanged("QPLAN", value); }
        }
        private string _GPPARTIE;
        public string GPPARTIE
        {
            get { return _GPPARTIE; }
            set { _GPPARTIE = value; OnPropertyChanged("GPPARTIE", value); }
        }

        public void QCNEW(Dictionary<string, object> pm)
        {
            RUNID = pm["RUNID"].ToString();
            TBMain.Clear();
            QPLAN = pm["QPLAN"].ToString();
            YCOUNT = pm["YCOUNT"].ToString();
            if (QPLAN.Length < 8)
            {
                return;
            }
            NEWQR(pm);
        }

        public void NEWQR(Dictionary<string, object> pm)
        {
            IsEdit = true;
            IsKEY = true;
            if (QPLAN.Length < 8)
                return;
            DataRow dwData;
            dwData = TBMain.NewRow();
            SetUpNewRow(ref dwData);
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "QORDER");
            dwData["QOKORDER"] = Helper.Right("000000000" + LFD, 9);
            if (pm["AUFTYP"].ToString() != "GB")
            {
                dwData["QOKAUFTYP"] = pm["AUFTYP"].ToString();
                dwData["QOKAUFKRS"] = pm["KRS"].ToString();
                dwData["QOKAUFLFD"] = pm["LFD"].ToString();
                dwData["QOKAUFPOS"] = pm["POS"].ToString();
                dwData["QOKAFOLGE"] = pm["LIEFSCH"].ToString();
                dwData["QOKTP"] = "0000";
                dwData["QOKPRODPART"] = Helper.Left(pm["PARTIE"].ToString().PadRight(11), 11);
            }
            else
            {
                dwData["QOKWEPARTIE"] = pm["PARTIE"].ToString();
                DataRow dwPartie = VGPDAT.Instance.TBMain.NewRow();
                dwPartie["GPMANDANT"] = app.Mandant;
                dwPartie["GPPARTIE"] = pm["PARTIE"].ToString();
                dwPartie["GPGEPRUEFT"] = "J";
                VGPDAT.Instance.UpdateGPDAT(dwPartie, 2);
            }
            dwData["QOKPNRKRS"] = QPLAN.Substring(0, 2);
            dwData["QOKPLFDNR"] = QPLAN.Substring(2, 6);
            dwData["QOKARTST"] = pm["P5"].ToString();
            dwData["QOKART"] = pm["ART"].ToString();
            dwData["QOKVAR"] = pm["VAR"].ToString();
            dwData["QOKDESS"] = pm["DESS"].ToString();
            dwData["QOKCOLORIT"] = pm["COLORIT"].ToString();
            dwData["QOKFARBE"] = pm["FARBE"].ToString();
            dwData["QOKGROE"] = pm["GROE"].ToString();
            dwData["QOKBESCHICH"] = pm["BESCHICH"].ToString();
            Insert(dwData);
            QCRELOAD(dwData);
            pm["QPLAN"] = QPLAN;
            pm["QORDER"] = dwData["QOKORDER"].ToString();
            ((DataRow)pm["ROWQP"])["QOKORDER"] = pm["QORDER"];
            VQOSTEP.Instance.NEWQCSTEP(pm);
        }

        public void QCRELOAD(DataRow _dwQC)
        {
            MessageInfo msg = new MessageInfo();
            msg.MSGTYPE = "QCRELOAD";
            msg.Sender = this.RUNID;
            msg.ACTION = "QCRELOAD";

            msg.ORDER = _dwQC["QOKORDER"].ToString();
            msg.PARTIE = _dwQC["QOKWEPARTIE"].ToString();
            if (_dwQC["QOKAUFTYP"].ToString() == "V")
            {
                msg.PARTIE = Helper.Left(_dwQC["QOKPRODPART"].ToString().PadRight(10), 10);
            }
            msg.P5 = _dwQC["QOKARTST"].ToString();
            msg.AUFTYP = _dwQC["QOKAUFTYP"].ToString();
            msg.KRS = _dwQC["QOKAUFKRS"].ToString();
            msg.LFD = _dwQC["QOKAUFLFD"].ToString();
            msg.POS = _dwQC["QOKAUFPOS"].ToString();
            msg.LIEFSCH = _dwQC["QOKAFOLGE"].ToString();
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }


        public void Insert(DataRow _dwQC)
        {
            if (_dwQC == null)
                return;
            _dwQC["QOKNEUB"] = app.User;
            _dwQC["QOKNEUD"] = date;
            InsertQORDER(_dwQC, app.company);
            TBMain.Rows.InsertAt(_dwQC, 0);
        }

        public void COPYQC(DataRow _dwQC, DataRow _dwReq)
        {
            if (_dwQC == null)
                return;
            DataRow dwTemp = TBMain.Clone().NewRow();
            dwTemp.ItemArray = _dwQC.ItemArray;
            TBMain.Clear();
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwTemp.ItemArray;
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "QORDER");
            dwData["QOKORDER"] = Helper.Right("000000000" + LFD, 9);
            Insert(dwData);
            QPLAN = dwData["QOKPNRKRS"].ToString() + dwData["QOKPLFDNR"].ToString();
            PM["RUNID"] = RUNID;
            if (_dwReq.Table.Columns.Contains("YCOUNT"))
            {
                PM["YCOUNT"] = _dwReq["YCOUNT"].ToString();
            }
            PM["QPLAN"] = QPLAN;
            PM["QORDER"] = dwData["QOKORDER"].ToString();
            PM["ROWQP"] = _dwReq;
            ((DataRow)PM["ROWQP"])["QOKORDER"] = PM["QORDER"];
            VQOSTEP.Instance.COPYQCSTEP(PM);
        }

        public void SAVEQP(DataRow _dwQP)
        {
            if (_dwQP == null)
                return;
            _dwQP["QOKAENB"] = app.User;
            _dwQP["QOKAEND"] = date;
            UpdateQORDER(_dwQP, app.company, 0);
        }

        public DataSet LOADQC(Dictionary<string, object> pm)
        {
            dwData = TBMain.NewRow();
            dwData["QOKMANDANT"] = app.Mandant;
            dwData["QOKPRODPART"] = (string)pm["QOKPRODPART"];
            dwData["QOKFILLER4"] = VQPMERKM.Instance.TTLQT();
            SResult = QueryQORDER(dwData, 5);
            return SResult;
        }  
    }
}
